<?php
/**
 * @title 34. 在排序数组中查找元素的第一个和最后一个位置
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[] $nums
     * @param Integer $target
     * @return Integer[]
     */
    function searchRange($nums, $target) {
        /**
         * @since 2024-01-01 数组数量
         */
        $numCount = count($nums);

        /**
         * @since 2024-01-01 全局数组
         */
        $this->nums = $nums;

        /**
         * @return [Integer, Integer]
         */
        return $this->FindTargetRange(0, $numCount-1, $target);
    }

    /**
     * @title 在区间[s,e]范围内，查找target的范围
     * @author start2004
     * @since 2024-01-01 Created
     *
     * @param Integer $s
     * @param Integer $e
     * @param Integer $target
     * @return [Integer, Integer]
     */
    function FindTargetRange($s, $e, $target){
        /**
         * @since 2024-01-01 二分查找
         */
        while($s<=$e){
            /**
             * @since 2024-01-01 中间位置
             */
            $m = ($s+$e)>>1;

            /**
             * @since 2024-01-01 找到目标值
             */
            if($this->nums[$m] == $target){
                /**
                 * @since 2024-01-01 左边、右边继续找target
                 */
                $leftRange = $this->FindTargetRange($s, $m-1, $target);
                $rightRange = $this->FindTargetRange($m+1, $e, $target);

                /**
                 * @since 2024-01-01 合并区间
                 */
                if($leftRange[0] != -1){
                    $left = $leftRange[0];
                } else {
                    $left = $m;
                }
                if($rightRange[1] != -1){
                    $right = $rightRange[1];
                } else {
                    $right = $m;
                }

                /**
                 * @return [Integer, Integer]
                 */
                return [$left, $right];
            } elseif($this->nums[$m] > $target){
                $e = $m-1;
            } elseif($this->nums[$m] < $target){
                $s = $m+1;
            } else {}
        }

        /**
         * @return [Integer, Integer]
         */
        return [-1, -1];
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.01_2.php
 */
$datas = [
    [[5,7,7,8,8,10], 8], // [3,4]
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->searchRange(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
